﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;

namespace hr_1.View
{
    /// <summary>
    /// Interaction logic for Employee_show_employee.xaml
    /// </summary>
    public partial class Edit_employee : Page
    {
        public List<Model.PersonComInf> personInf = new List<Model.PersonComInf>();
        public HRDataContext hrData = new HRDataContext();
        public Edit_employee()
        {
            InitializeComponent();
            dataGrid_persons.ItemsSource = hrData.persons;
            //dataGrid_edu.ItemsSource = hrData.educations;
            //dataGrid_military.ItemsSource = hrData.militaries;
            //dataGrid_workActivity.ItemsSource = hrData.work_activities;
        }

        public Edit_employee(int selectedId)
        {
            InitializeComponent();
            var query1 = from temp1 in hrData.persons
                         where selectedId == temp1.personId
                         select new
                         {
                             temp1.personId,
                             temp1.firstname,
                             temp1.middlename,
                             temp1.lastname,
                             temp1.birthday,
                             temp1.sex,
                             temp1.date_begin_work
                         };
            personInf = new List<Model.PersonComInf>();
            foreach (var inf in query1)
            {
                personInf.Add(new Model.PersonComInf
                    (inf.personId, inf.firstname, inf.middlename, inf.lastname,
                    inf.birthday, inf.sex, inf.date_begin_work));
            }
            dataGrid_persons.ItemsSource = personInf;
        }

        private void Save_Click(object sender, RoutedEventArgs e)
        {
            foreach (var inf in personInf)
            {
                var query_update = from upinf in hrData.persons
                                   where upinf.personId == inf.id
                                   select upinf;
                foreach (var upinf in query_update)
                {
                    upinf.firstname = inf.firstname;
                    upinf.middlename = inf.middlename;
                    upinf.lastname = inf.lastname;
                    upinf.birthday = inf.birthday;
                    upinf.sex = inf.sex;
                    upinf.date_begin_work = inf.date_begin_work;
                }
            }
            hrData.SubmitChanges();
            MessageBox.Show("Изменения сохранены");
        }

        private void Cancel_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("View/MainPage.xaml", UriKind.Relative));
        }
    }
}
